package com.velvetantler.weather

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.velvetantler.weather.adapter.ViewPagerAdapter

class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"
    private var mCurrentPage = 0
    private var mViewPager: ViewPager2? = null
    private var navHome: View? = null
    private var navStar: View? = null
    private var navMine: View? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.ac_main)
        // 初始化界面
        initView()
    }

    fun initView() {
        // 给ViewPager2绑定适配器
        mViewPager = findViewById<ViewPager2>(R.id.vpMain)
        mViewPager?.adapter = ViewPagerAdapter(this)
        // 获取底部导航栏控件
        val tabBar = findViewById<View>(R.id.llBottomNavigation)
        navHome = tabBar.findViewById<View>(R.id.nav_home)
        navStar = tabBar.findViewById<View>(R.id.nav_life)
        navMine = tabBar.findViewById<View>(R.id.nav_profile)
        // 给tabBar设置点击事件
        addClickEvent()
        // 给ViewPager2设置页面切换监听
        addViewPagerChangeListener()
    }

    fun addClickEvent() {
        // 点击改变tabBar的选中项和更换Fragment
        navHome?.setOnClickListener {
            if (mCurrentPage == 0) {
                return@setOnClickListener
            }
            mCurrentPage = 0
            changeTabBarSelected()
            mViewPager?.setCurrentItem(mCurrentPage, true)
        }
        navStar?.setOnClickListener {
            if (mCurrentPage == 1) {
                return@setOnClickListener
            }
            mCurrentPage = 1
            changeTabBarSelected()
            mViewPager?.setCurrentItem(mCurrentPage, true)
        }
        navMine?.setOnClickListener {
            if (mCurrentPage == 2) {
                return@setOnClickListener
            }
            mCurrentPage = 2
            changeTabBarSelected()
            mViewPager?.setCurrentItem(mCurrentPage, true)
        }
    }

    fun addViewPagerChangeListener() {
        mViewPager?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (mCurrentPage != position) {
                    // 改变tabBar的选中项
                    mCurrentPage = position
                    changeTabBarSelected()
                }
            }
        })
    }

    fun changeTabBarSelected() {
        val imgHome = navHome?.findViewById<ImageView>(R.id.img_home)
        val tvHome = navHome?.findViewById<TextView>(R.id.tv_home)
        val imgStar = navStar?.findViewById<ImageView>(R.id.img_life)
        val tvStar = navStar?.findViewById<TextView>(R.id.tv_life)
        val imgMine = navMine?.findViewById<ImageView>(R.id.img_profile)
        val tvMine = navMine?.findViewById<TextView>(R.id.tv_profile)
        when (mCurrentPage) {
            0 -> {
                imgHome?.setColorFilter(R.color.weather_primary)
                tvHome?.setTextColor(resources.getColor(R.color.weather_primary, null))
                imgStar?.setColorFilter(R.color.default_gray)
                tvStar?.setTextColor(resources.getColor(R.color.default_gray, null))
                imgMine?.setColorFilter(R.color.default_gray)
                tvMine?.setTextColor(resources.getColor(R.color.default_gray, null))
            }

            1 -> {
                imgStar?.setColorFilter(R.color.weather_primary)
                tvStar?.setTextColor(resources.getColor(R.color.weather_primary, null))
                imgHome?.setColorFilter(R.color.default_gray)
                tvHome?.setTextColor(resources.getColor(R.color.default_gray, null))
                imgMine?.setColorFilter(R.color.default_gray)
                tvMine?.setTextColor(resources.getColor(R.color.default_gray, null))
            }

            2 -> {
                imgMine?.setColorFilter(R.color.weather_primary)
                tvMine?.setTextColor(resources.getColor(R.color.weather_primary, null))
                imgHome?.setColorFilter(R.color.default_gray)
                tvHome?.setTextColor(resources.getColor(R.color.default_gray, null))
                imgStar?.setColorFilter(R.color.default_gray)
                tvStar?.setTextColor(resources.getColor(R.color.default_gray, null))

            }
        }
    }

}